﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace OpenPhotoAlbum.Controls.PhotoProcessors
{
    public class RoundedCorners : IPhotoProcessor
    {
        private const int RADIUS = 35;
        private const int DIAMETER = RADIUS * 2;

        #region IPhotoProcessor Members

        public byte[] Process(byte[] input)
        {
            MemoryStream ms = new MemoryStream(input);
            Bitmap source = new Bitmap(ms);
            int width = source.Width;
            int height = source.Height;
            Bitmap canvas = new Bitmap(width, height);

            Graphics g = Graphics.FromImage(canvas);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            g.DrawImage(source, 0, 0, width, height);

            {
                Region r = new Region(Rectangle.FromLTRB(0, 0, RADIUS, RADIUS));
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(0, 0, DIAMETER, DIAMETER);
                r.Exclude(gp);
                g.FillRegion(Brushes.White, r);
            }
            {
                Region r = new Region(Rectangle.FromLTRB(width - RADIUS, 0, width, RADIUS));
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(width - DIAMETER - 1, 0, DIAMETER, DIAMETER);
                r.Exclude(gp);
                g.FillRegion(Brushes.White, r);
            }
            {
                Region r = new Region(Rectangle.FromLTRB(0, height - RADIUS, RADIUS, height));
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(0, height - DIAMETER, DIAMETER, DIAMETER);
                r.Exclude(gp);
                g.FillRegion(Brushes.White, r);
            }
            {
                Region r = new Region(Rectangle.FromLTRB(width - RADIUS, height - RADIUS, width, height));
                GraphicsPath gp = new GraphicsPath();
                gp.AddEllipse(width - DIAMETER - 1, height - DIAMETER, DIAMETER, DIAMETER);
                r.Exclude(gp);
                g.FillRegion(Brushes.White, r);
            }

            g.Dispose();

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            //find the encoder with the image/jpeg mime-type
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                    ici = codec;
            }

            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, BusinessLogics.PhotoManager.JPEG_QUALITY);

            MemoryStream msResult = new MemoryStream();
            canvas.Save(msResult, ici, ep);

            canvas.Dispose();
            source.Dispose();
            ms.Close();

            return msResult.ToArray();
        }

        #endregion
    }
}
